﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;

namespace svc
{
    public class EmguCvPtr : IDisposable
    {
        private IntPtr _ptr;
        private Action<IntPtr> _deleter;


        public EmguCvPtr(IntPtr data, Action<IntPtr> deleter)
        {
            _ptr = data;
            _deleter = deleter;
        }

        
        public IntPtr Ptr
        {
            get
            {
                return _ptr;
            }
        }
        public bool IsValid
        {
            get
            {
                return _ptr != IntPtr.Zero;
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                //release managed resources
            }
            if (_ptr != IntPtr.Zero)
            {
                _deleter(_ptr);
                _ptr = IntPtr.Zero;
            }
        }
        public void Dispose()
        {
            Dispose(true);
        }
    }

    public class ImageFrame : EmguCvPtr
    {
        public ImageFrame(IntPtr data) : base(data, p => CvInvoke.cvReleaseImage(ref p))
        {
        }

        public static ImageFrame Create(int width, int height, Emgu.CV.CvEnum.IPL_DEPTH depth = Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, int channels = 1)
        {
            return new ImageFrame(CvInvoke.cvCreateImage(new System.Drawing.Size(width, height), depth, channels));
        }
    }
    public class ImageCapture : EmguCvPtr
    {
        public ImageCapture(IntPtr data)
            : base(data, p => CvInvoke.cvReleaseCapture(ref p))
        {
        }

        public ImageFrame QueryFrame()
        {
            return new ImageFrame(CvInvoke.cvQueryFrame(base.Ptr));
        }
    }

    public class Matrix : EmguCvPtr
    {
        public Matrix(IntPtr data)
            : base(data, p => CvInvoke.cvReleaseMat(ref p))
        {
        }
    }
}
